package com.itruth.module.record;

import java.sql.Timestamp;
import java.util.Timer;
import java.util.TimerTask;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.itruth.base.BaseActivity;
import com.itruth.data.dao.ApperceptionDao;
import com.itruth.data.dao.DraftDao;
import com.itruth.data.entity.Apperception;
import com.itruth.data.entity.UserData;
import com.itruth.main.R;
import com.itruth.util.CommonUtil;

/**
 * 记录页面
 *
 */
public class RecordActivity extends BaseActivity {
	
	private EditText editText;
	
	private String draft;
	
	private Timer timer;
	
	/**
	 * 用户点击保存，无需在onPause()方法里再去更新草稿，只要置空即可
	 */
	private boolean isSave;
	
	@Override
	protected void initData() {
		
		timer = new Timer();
		draft = DraftDao.queryDraft();
		
	}
	
	@Override
	protected void initComposition() {
		setContentView(R.layout.record);
		
		Button saveBtn = (Button) findViewById(R.id.btn_save);
		Button backBtn = (Button) findViewById(R.id.btn_back);
		editText = (EditText) findViewById(R.id.et_record);
		
		editText.setText(draft);
		editText.setSelection(editText.getText().toString().length());
		
		editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				//开始输入时，启动定时器，每30秒更新草稿数据
				startTimer();
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		
		saveBtn.setOnClickListener(listener);
		backBtn.setOnClickListener(listener);
	}
	
	private View.OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
				
				case R.id.btn_back :
					finish();
					break;
				case R.id.btn_save :
					saveAppe();
					break;
				default :
					break;
			}
			
		}
	};
	
	/**
	 * 保存用户的输入
	 */
	private void saveAppe() {
		
		String temp = editText.getEditableText().toString().trim();
		
		if (!CommonUtil.isEmpty(temp)) {
			//保存内容，删除草稿(将editText内容置空，更新时即可置空)
			ApperceptionDao.insert(generateAppe(temp));
			UserData.getIns().setWriteTimes(UserData.getIns().getWriteTimes() + 1);
			isSave = true;
			finish();
		} else {
			Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	private Apperception generateAppe(String content) {
		
		Apperception appe = new Apperception();
		appe.setContent(content);
		appe.setTime(new Timestamp(System.currentTimeMillis()));
		return appe;
	}
	
	/**
	 * 保存草稿到数据库
	 */
	private void saveDraft(boolean isSave) {
		DraftDao.saveDraft(isSave ? "" : editText.getEditableText().toString().trim());
	}
	
	/**
	 * 启动定时器，定时保存草稿
	 */
	private void startTimer() {
		
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				saveDraft(false);
			}
		}, 0, 30000);
	}
	
	/**
	 * 关闭定时器
	 */
	private void stopTimer() {
		if (null != timer) {
			Log.i("lanyan", "取消更新");
			timer.purge();
			timer.cancel();
		}
	}
	
	@Override
	protected void onPause() {
		stopTimer();
		saveDraft(isSave);
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	
	
}
